<template>
	<view class="content">		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">钱包名称</view>
			<view class="lable-select" style="width: 80%;">
				<input type="text" v-model="form.qb_name" style="font-size: 14px;" placeholder="请输入钱包名称"/>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">钱包地址</view>
			<view class="lable-select" style="width: 80%;">
				<input type="digit" v-model="form.qb_address" style="font-size: 14px;" placeholder="请输入钱包地址"/>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 30%;">上传二维码</view>
			<view class="lable-select" style="width: 70%;">
				<uni-file-picker v-model="form.qb_qrcode_url" limit="1" file-mediatype="image" file-extname="png,jpg,jpeg" @select="uploadImg"></uni-file-picker>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">交易密码</view>
			<view class="lable-select" style="width: 80%;">
				<input type="safe-password" v-model="form.pay_pass" style="font-size: 14px;" placeholder="交易密码"/>
			</view>
		</view>
	</view>
	
	<view class="content" style="padding: 1rem;text-align: center;">
		<button style="width: 90%;background: #dd5154;color: white;height: 2.5rem; line-height: 2.5rem;" @click="doFinance()">确认提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:null,
				form:{
					qb_name:null,
					qb_address:null,
					qb_qrcode:null,
					pay_pass:null,
					qb_qrcode_url:null
				},
				imageValue:[],
				user_token: null
			};
		},
		onLoad() {
			this.getUserInfo()
			this.getFinance()
		},
		methods:{
			getUserInfo(){
				let that = this
				if(that.userInfo == null){
					uni.getStorage({
						key: 'user_token',
						success: function (res) {
							that.user_token = res.data
							that.checkUser()
						},
						fail: function(res){
							console.log(res)
							uni.showToast({
								title:'请先登录',
								icon:'none',
								success() {
									uni.redirectTo({
										url:'/pages/login/login',
									})
								}
							})
							
						}
					});
				}
			},
			checkUser(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getUserInfo',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						if(res.data.code == 401){
							uni.removeStorage({
								key:'user_token',
								success() {
									uni.showToast({
										icon:'none',
										title:'登录已失效,请重新登录',
										success() {
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/login/login'
												})
											})
										}
									})
								}
							})
						}
						that.userInfo = res.data.data
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			getFinance(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getFinance',
					header:{token:that.user_token},
					method:'POST',
					success(res) {
						if(res.data.code == 1){
							that.form = res.data.data
							that.form.pay_pass = null
							console.log(that.form.qb_qrcode != null)
							if(that.form.qb_qrcode != null){
								that.form.qb_qrcode_url = [{
									name:'ceshi.jpg',
									extname:'jpg',
									url:getApp().globalData.baseUrl + that.form.qb_qrcode
								}]
							}
							
						}
					}
				})
			},
			doFinance(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/doFinance',
					header:{token:that.user_token},
					data:that.form,
					method:'POST',
					success(res) {
						uni.showToast({
							icon:'none',
							title:res.data.msg,
							success(res) {
								setTimeout(function(){
									uni.switchTab({
										url:'/pages/my/my'
									})
								},1000)
							}
						})
					}
				})
			},
			uploadImg(data){
				let that = this
				uni.uploadFile({
					url: getApp().globalData.baseUrl + '/api/index/upload',
					filePath: data.tempFilePaths[0],
					success(res) {
						that.form.qb_qrcode = res.data
						that.form.qb_qrcode_url = [{
							name:'ceshi.jpg',
							extname:'jpg',
							url:getApp().globalData.baseUrl + res.data
						}]
						console.log(that.form.qb_qrcode_url)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #f4f4f4;
	}
	.xuaz{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 0 0.6rem;
		padding: 1rem;
		border-bottom: 1px solid #f6f6f6 !important;
	}
	.lable-name{
		width: 30%;
		font-weight: bold;
		font-size: 14px;
	}
	.lable-select{
		width: 60%;
		display: flex;
	}
	.content{
		background: #fff;
		display: flex;
		flex-direction: column;
		margin-top: .7rem;
	}
	
	.lable-select >input{
		width: 100%;
	}
</style>
